package liy.demo.wikiproject.controller;

import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.Valid;
import liy.demo.wikiproject.exception.BookException;
import liy.demo.wikiproject.exception.BookExceptionCode;
import liy.demo.wikiproject.req.UserQueryReq;
import liy.demo.wikiproject.req.UserSaveReq;
import liy.demo.wikiproject.resp.CommonResp;
import liy.demo.wikiproject.resp.UserResp;
import liy.demo.wikiproject.resp.PageResp;
import liy.demo.wikiproject.service.EBookService;
import liy.demo.wikiproject.service.UserService;
import org.springframework.web.bind.annotation.*;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Executors;

@RestController
@RequestMapping("/user")
public class UserController {
    @Resource
    private UserService userService;
    @PostMapping("login")
    public CommonResp<UserResp> login(@RequestBody UserQueryReq req) {
        CommonResp<UserResp> resp = new CommonResp<>();
        UserResp validate = userService.validate(req);
        resp.setContent(validate);
        resp.setSuccess(true);
        return resp;
    }

    @RequestMapping("logout")
    public CommonResp<String> logout(HttpServletRequest servletRequest) {
        String token = (String)servletRequest.getAttribute("token");
        userService.delete(token);
        CommonResp<String>commonResp = new CommonResp<>();
        commonResp.setSuccess(true);
        commonResp.setContent("删了");
        return  commonResp;
    }

    @RequestMapping("info")
    public CommonResp<UserResp> info(HttpServletRequest servletRequest) {
        String token = (String)servletRequest.getAttribute("token");

        UserResp info = userService.info(token);


        CommonResp<UserResp>commonResp = new CommonResp<>();
        commonResp.setSuccess(true);
        commonResp.setContent(info);
        return commonResp;
    }

}
